﻿
using CSharpLibs.Annotations;

namespace System.IO
{
	[Builtin]
	public static class File
	{
		public static void Copy(string s, string d)
		{ Copy(s, d, false); }

		[BuiltinMethod, BuiltinSemantics("file_copy")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public static void Copy(string s, string d, bool flag)
		{ ;}

		public static FileStream Create(string path)
		{ return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None); }
		
		[BuiltinMethod, BuiltinSemantics("file_delete")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public static void Delete(string s)
		{ ;}

		[BuiltinMethod, BuiltinSemantics("file_exists")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public static bool Exists(string s)
		{ return false; }

		[BuiltinMethod, BuiltinSemantics("file_move")]
		[UsedModStatics("System.ExternalState.fsstate")]
		public static void Move(string s, string d)
		{ ;}

		public static FileStream Open(string path, FileMode mode)
		{ return new FileStream(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None); }

		public static FileStream OpenRead(string path)
		{ return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); }

		public static StreamReader OpenText(string path)
		{ return new StreamReader(path); }

		public static FileStream OpenWrite(string path)
		{ return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); }

		public static string[] ReadAllLines(String s)
		{
			StreamReader fs = new StreamReader(s);
			Collections.Generic.List<String> lines = new Collections.Generic.List<String>();
			String ts;

			ts = fs.ReadLine();
			while(ts != null)
			{
				lines.Add(ts);
				ts = fs.ReadLine();
			}

			fs.Close();
			return lines.ToArray();
		}

		public static String ReadAllText(String s)
		{
			StreamReader fs = OpenText(s);
			String ts = fs.ReadToEnd();
			fs.Close();
			return ts;
		}

		public static void WriteAllLines(string s, string[] lines)
		{
			StreamWriter fs = new StreamWriter(s);

			for(int i = 0; i < lines.Length; ++i)
				fs.WriteLine(lines[i]);

			fs.Close();
		}

		public static void WriteAllText(string s, string txt)
		{
			StreamWriter fs = new StreamWriter(s);
			fs.Write(txt);
			fs.Close();
		}
	}
}
